package com.camera.yl.widght;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class RecordButton extends View {

    private Paint outerCirclePaint;
    private Paint innerCirclePaint;
    private boolean isRecording = false;
    private OnRecordClickListener onRecordClickListener;

    public RecordButton(Context context) {
        super(context);
        init();
    }

    public RecordButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RecordButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        outerCirclePaint = new Paint();
        outerCirclePaint.setAntiAlias(true);
        outerCirclePaint.setStyle(Paint.Style.STROKE);
        outerCirclePaint.setStrokeWidth(8); // 外圈宽度
        outerCirclePaint.setColor(Color.WHITE); // 录制时的内圈颜色

        innerCirclePaint = new Paint();
        innerCirclePaint.setAntiAlias(true);
        innerCirclePaint.setStyle(Paint.Style.FILL);
        innerCirclePaint.setColor(Color.RED); // 录制时的内圈颜色

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击按钮时切换录制状态
                isRecording = !isRecording;
                invalidate(); // 重绘视图

                // 触发回调接口
                if (onRecordClickListener != null) {
                    onRecordClickListener.onRecordClick(isRecording);
                }
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        int radius = Math.min(centerX, centerY) - 4;

        // 绘制外圈
        canvas.drawCircle(centerX, centerY, radius, outerCirclePaint);

        // 绘制内圈（录制状态下为红色，否则为白色）
        if (isRecording) {
            canvas.drawCircle(centerX, centerY, radius / 2, innerCirclePaint);
        }
    }

    public void setOnRecordClickListener(OnRecordClickListener listener) {
        this.onRecordClickListener = listener;
    }

    public interface OnRecordClickListener {
        void onRecordClick(boolean isRecording);
    }

    public void setRecording(boolean recording) {
        isRecording = recording;
        invalidate(); // 重绘视图
    }
}

